import QtQuick 2.2
import FileIO 1.0

Image {
    id: pwu
    source: "LIFT/ui1/bk.png"
    width: 768
    height: 1024
    property int pwd: 0
    property int inppos: 0
    property string inpstr1
    property string inpstr2
    property string inpstr3
    property string inpstr4
    property string inpstrt1
    property string inpstrt2
    property string inpstrt3
    property string inpstrt4
    property var xmldata
    signal start;
    signal keypress(int key)

    property var arr_cn:["请输入密码","请输入原始密码","请输入新密码","请再次输入新密码"];
    property var arr_en:["PLEASE ENTER PASSWORD","PLEASE ENTER OLD PASSWORD","PLEASE ENTER NEW PASSWORD","PLEASE ENTER NEW PASSWORD AGAIN"];

    Item{
        x:160
        y:88
        width: 450
        height: 90
        opacity: 1
        Row {
            id: row0
            anchors { fill: parent;topMargin: 1; leftMargin: 1}
            spacing: 32

            Button{id:inp1;fsize:40;text0: "";width: 88;height: 88;onBtclicked:{btreply(str)}}
            Button{id:inp2;fsize:40;text0: "";width: 88;height: 88;onBtclicked:{btreply(str)}}
            Button{id:inp3;fsize:40;text0: "";width: 88;height: 88;onBtclicked:{btreply(str)}}
            Button{id:inp4;fsize:40;text0: "";width: 88;height: 88;onBtclicked:{btreply(str)}}
        }
    }

    Item{
        x:158
        y:214
        opacity: 1
        Column{
            Text {
                id: name
                font.family: "WenQuanYi Micro Hei Mono"
                font.pixelSize:36
                font.bold: true
                color: "#755A30";
                text: arr_cn[0]
            }

            Text {
                id: name1
                //font.family: "WenQuanYi Micro Hei Mono"
                font.pixelSize:20
                font.bold: true
                color: "#755A30";
                text: arr_en[0]
            }
        }

    }

    Item{
        x:111
        y:344
        width: 545
        height: 464
        Row {
            id: row1
            anchors { fill: parent; topMargin: 30; leftMargin: 52; }
            spacing: 36

            Button{text: "1";source0: "LIFT/ui1/1.png";source1: "LIFT/ui1/1_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "2";source0: "LIFT/ui1/2.png";source1: "LIFT/ui1/2_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "3";source0: "LIFT/ui1/3.png";source1: "LIFT/ui1/3_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "K";source0: "LIFT/ui1/open.png";source1:"LIFT/ui1/open_dn.png";onBtclicked:{keypress(9)}}
        }

        Row {
            id: row2
            anchors { fill: parent; topMargin: 134; leftMargin: 52; }
            spacing: 36

            Button{text: "4";source0: "LIFT/ui1/4.png";source1:"LIFT/ui1/4_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "5";source0: "LIFT/ui1/5.png";source1: "LIFT/ui1/5_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "6";source0: "LIFT/ui1/6.png";source1: "LIFT/ui1/6_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "G";source0: "LIFT/ui1/close.png";source1: "LIFT/ui1/close_dn.png";onBtclicked:{keypress(10)}}
        }

        Row {
            id: row3
            anchors { fill: parent; topMargin: 238; leftMargin: 52; }
            spacing: 36

            Button{text: "7";source0: "LIFT/ui1/7.png";source1: "LIFT/ui1/7_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "8";source0: "LIFT/ui1/8.png";source1: "LIFT/ui1/8_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "9";source0: "LIFT/ui1/9.png";source1: "LIFT/ui1/9_dn.png";onBtclicked:{btreply(str)}}
            Image {
                id: bell
                source: "LIFT/ui1/bell.png"
                MouseArea {
                    id: mouseArea1
                    anchors.fill: parent

                    onClicked:{
                        root.bellen = true
                        if(root.jflag)
                        {
                            bell.source="LIFT/ui1/bell.png"
                            keypress(16)
                        }
                        else
                        {
                            bell.source= "LIFT/ui1/bell_dn.png"
                            keypress(15);
                        }
                        //root.jflag = !root.jflag
                        timerdl2.start()
                    }

                }
            }
        }

        Row {
            id: row4
            anchors { fill: parent; topMargin: 342; leftMargin: 52; }
            spacing: 36

            Button{text: "S";source0: "LIFT/ui1/set.png";source1: "LIFT/ui1/set_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "0";source0: "LIFT/ui1/0.png";source1: "LIFT/ui1/0_dn.png";onBtclicked:{btreply(str)}}
            Button{text: "D";source0: "LIFT/ui1/del.png";source1: "LIFT/ui1/del_dn.png";onBtclicked:{btreply(str)}}
        }
   }

    Timer {
         id: timerdl2;
         interval: 500;
         running: false;
         repeat: false;
         triggeredOnStart: false;
         onTriggered:{
                root.bellen = false
             }
         }

  Timer{
        id: timer;
        interval: 500;
        running: false
        repeat: false;
        triggeredOnStart: false;
        onTriggered:{
            inppos = 0;
            if (pwd==0)
            {
                if( (inpstr1===xmldata.P01)&&(inpstr2===xmldata.P02)&&(inpstr3===xmldata.P03)&&(inpstr4 === xmldata.P04) ){
                    console.log("ok")

                        inp1.text0 = ""
                        inp2.text0= ""
                        inp3.text0 = ""
                        inp4.text0 = ""
                        pwu.start();

                }else{
                    console.log("erro")
                    inp1.text0 = ""
                    inp2.text0 = ""
                    inp3.text0 = ""
                    inp4.text0 = ""
                }
            }
            else if (pwd==3){
                pwd =0
                name.text = arr_cn[0]
                name1.text = arr_en[0]
                name.color="#755A30"
                name1.color="#755A30"
                 if( (inpstr1===inpstrt1)&&(inpstr2===inpstrt2)&&(inpstr3===inpstrt3)&&(inpstr4 === inpstrt4) ){
                     console.log("ok")
                     xmldata.P01 = inpstr1
                     xmldata.P02 = inpstr2
                     xmldata.P03 = inpstr3
                     xmldata.P04 = inpstr4
                     inp1.text0 = ""
                     inp2.text0 = ""
                     inp3.text0 = ""
                     inp4.text0 = ""
                     saveDocument()
                 }
                 else{
                     console.log("erro")

                     inp1.text0 = ""
                     inp2.text0 = ""
                     inp3.text0 = ""
                     inp4.text0 = ""
                 }
             }
            else if (pwd==2){
                pwd=3
                name.text = arr_cn[3]
                name1.text = arr_en[3]
                inpstrt1=inpstr1
                inpstrt2=inpstr2
                inpstrt3=inpstr3
                inpstrt4=inpstr4
                inp1.text0 = ""
                inp2.text0= ""
                inp3.text0 = ""
                inp4.text0 = ""
            }
            else if (pwd==1){
                if( ( (inpstr1===xmldata.P01)&&(inpstr2===xmldata.P02)&&(inpstr3===xmldata.P03)&&(inpstr4 === xmldata.P04))||
                       ( (inpstr1==="3")&&(inpstr2==="2")&&(inpstr3==="1")&&(inpstr4 === "0")) ){
                        pwd=2
                        console.log("ok")
                        name.text = arr_cn[2]
                        name1.text = arr_en[2]

                        inp1.text0 = ""
                        inp2.text0= ""
                        inp3.text0 = ""
                        inp4.text0 = ""
                }else{
                    console.log("erro")
                    pwd=0
                    inp1.text0 = ""
                    inp2.text0 = ""
                    inp3.text0 = ""
                    inp4.text0 = ""
                    name.text = arr_cn[0]
                    name1.text = arr_en[0]
                    name.color="#755A30"
                    name1.color="#755A30"
                }
            }
        }
    }

  FileIO {
         id: io
         source: "liftcfg.json"
  }

   function btreply(str) {
       if(str==="")
           return
       else if(str==="S"){
           pwd=1
           inppos=0
           inp1.text0 = ""
           inp2.text0= ""
           inp3.text0 = ""
           inp4.text0 = ""
           name.text = arr_cn[1]
           name1.text = arr_en[1]
           name.color="red"
           name1.color="red"
           return
       }
       else if(str==="D"){
           if(inppos>0)
               inppos--
            if(inppos===0){
               inp1.text0 = ""
            }
            else if(inppos===1){
                 inp2.text0 = ""
            }
            else if(inppos===2){
                inp3.text0 = ""
            }
       }else{
          if(inppos===0){
              inp1.text0 = "*"
              inpstr1 = str
          }
          else if(inppos===1){
              inp2.text0 = "*"
               inpstr2 = str
          }
          else if(inppos===2){
              inp3.text0 = "*"
               inpstr3 = str
          }
          else if(inppos===3){
              inp4.text0 = "*"
              inpstr4 = str
          }
          else{
              inp1.text0 = ""
              inp2.text0 = ""
              inp3.text0 = ""
              inp4.text0 = ""
          }

          inppos++
          if(inppos>=4){
              timer.start()
         }
       }
   }

   function bellkey(str){
       if( str===16 )
           bell.source="LIFT/ui1/bell.png"
       else if( str===15 )
           bell.source= "LIFT/ui1/bell_dn.png"
   }

   function readDocument() {
         io.read()
         xmldata= JSON.parse(io.text)
//          xmldata  = eval("("+io.text+")");
     }

     function saveDocument() {
         var data = xmldata
         io.text = JSON.stringify(data)
         io.write()
     }

     Component.onCompleted: {
        readDocument()
     }

}
